﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using System.Collections.ObjectModel;
using System.Windows.Media.Imaging;
using System.IO;


namespace CommunitySystem
{
	public partial class PersonnelInfoWindow : ChildWindow
	{
        CommunitySystem.PublicOfficialSelectionService.PublicOfficialSelectionServiceClient client = null;
        private int _positionid = 0;
        private string _name = string.Empty;
        private string _idcard = string.Empty;
        private string _type = string.Empty;
        private int _PublicOfficialRecordID = 0;
	    public string isLock = "";
        private int _empid = 0;
        public PersonnelInfoWindow(int positionid, string name, string idcard, int status, int PublicOfficialRecordID, string type, string islocked)
		{
			// 为初始化变量所必需
			InitializeComponent();
            _positionid = positionid;
            _name = name;
            _idcard = idcard;
            _type = type;
            isLock = islocked;
            if (_empClient == null)
                _empClient = new EmpbasicService.EmpBasicServiceClient();
            _empClient.ExportPersonnelInfoPositionCompleted += (sender, e1) =>//导出简历
            {
                //保存结果
                if (e1.Error == null)
                {
                    string serverpath = _empClient.InnerChannel.Via.AbsoluteUri.Replace(_empClient.InnerChannel.Via.AbsolutePath, "");
                    System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(serverpath + "/"+e1.Result, UriKind.RelativeOrAbsolute));
                }
                else
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("保存失败,可能原因:\n" + e1.Error);
                }
            };
            _PublicOfficialRecordID = PublicOfficialRecordID;
            client = new PublicOfficialSelectionService.PublicOfficialSelectionServiceClient();
            client.GetPublicOfficialSelectionCompleted += new EventHandler<PublicOfficialSelectionService.GetPublicOfficialSelectionCompletedEventArgs>(client_GetPublicOfficialSelectionCompleted);
            client.GetPublicOfficialSelectionAsync("0", positionid, 0, 0, -1, 0, islocked, 0);
            client.GetEmp_BasicInfoRegisterCompleted += new EventHandler<PublicOfficialSelectionService.GetEmp_BasicInfoRegisterCompletedEventArgs>(client_GetEmp_BasicInfoRegisterCompleted);
            client.GetEmp_BasicInfoRegisterAsync(name, idcard, positionid);
            client.CheckCompleted += new EventHandler<PublicOfficialSelectionService.CheckCompletedEventArgs>(client_CheckCompleted);
            if (status == 2 || status == 3 || status == 4)
            {
                btnpass.IsEnabled = false;
                btnfailed.IsEnabled = false;
                btnpass.Visibility = System.Windows.Visibility.Visible;
                btncancelpass.Visibility = System.Windows.Visibility.Collapsed;
            }
            if (status == 1)
            {
                btnpass.Visibility = System.Windows.Visibility.Collapsed;
                btncancelpass.Visibility = System.Windows.Visibility.Visible;
                btnfailed.IsEnabled = false;
            }
		}

        void client_CheckCompleted(object sender, PublicOfficialSelectionService.CheckCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("成功!");
                this.DialogResult = true;
            }
            else
                SilverlightMessageBoxLibrary.Message.ErrorMessage("失败:" + _name + "已有审核通过岗位， 不能再通过!");
        }

        void client_GetEmp_BasicInfoRegisterCompleted(object sender, PublicOfficialSelectionService.GetEmp_BasicInfoRegisterCompletedEventArgs e)
        {
            this.DataContext = e.Result;
            _empid = e.Result.ID;
            if (e.Result.HasPhoto == true)
            {
                ObservableCollection<byte> context = new ObservableCollection<byte>();
                client.DownLoadFileAsync( new PublicOfficialSelectionService.DownLoadFile(){ Content = null, FileName= e.Result.PhotoName, StartPosition=0, Lenght=1600, HasFinished = false, PathKey="photo"});
                client.DownLoadFileCompleted += (sender1, e1) =>
                    {
                        CommunitySystem.PublicOfficialSelectionService.DownLoadFile file = e1.Result;
                        if (file == null)
                        {
                            SilverlightMessageBoxLibrary.Message.ErrorMessage(e.Result.Name + "的照片错误，请联系管理员!");
                            return;
                        }
                        foreach (byte b in file.Content)
                        {
                            context.Add(b);
                        }
                        if (file.HasFinished == true)
                        {
                            BitmapImage b = new BitmapImage();
                            Stream mm = new MemoryStream(context.ToArray<byte>());
                            //mm.Write(_item.icon, 0, _item.icon.Length);
                            b.SetSource(mm);
                            img.Source = b;
                        }
                        else
                        {
                            file.StartPosition += file.Lenght;
                            client.DownLoadFileAsync(file);
                        }
                    };
            }
            ResetField();
        }
        void ResetField()
        {
            dtbFullDegree.Field = dtbFullDegree.Field;
            dtbFullEducationBg.Field = dtbFullEducationBg.Field;
            dtbGender.Field = dtbGender.Field;
            dtbHealth.Field = dtbHealth.Field;
            dtbNative.Field = dtbNative.Field;
            dtbPartDegree.Field = dtbPartDegree.Field;
            dtbPartEducationBg.Field = dtbPartEducationBg.Field;
            dtbPolitical.Field = dtbPolitical.Field;
        }

        void client_GetPublicOfficialSelectionCompleted(object sender, PublicOfficialSelectionService.GetPublicOfficialSelectionCompletedEventArgs e)
        {
            ObservableCollection<CommunitySystem.PublicOfficialSelectionService.PublicOfficialSelectionModel> position = e.Result;
            if (position.Count == 1)
            {
                tbDW.Text = position[0].OrgName;
                tbDepartMent.Text = position[0].Department;
                tbPosition.Text = position[0].Duty;
            }
        }
        /// <summary>
        /// 取消审核
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            if (isLock == "1")
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("该职位已锁定!");
                return;
            }
            if (MessageBox.Show("确定取消审核吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                client.CancelCheckPublicOfficialRecordModelCompleted -=
                    new EventHandler<PublicOfficialSelectionService.CancelCheckPublicOfficialRecordModelCompletedEventArgs>(client_CancelCheckPublicOfficialRecordModelCompleted);
                client.CancelCheckPublicOfficialRecordModelCompleted += 
                    new EventHandler<PublicOfficialSelectionService.CancelCheckPublicOfficialRecordModelCompletedEventArgs>(client_CancelCheckPublicOfficialRecordModelCompleted);
                client.CancelCheckPublicOfficialRecordModelAsync("(" + _PublicOfficialRecordID.ToString() + ")", _type);
            }
        }

        void client_CancelCheckPublicOfficialRecordModelCompleted(object sender, PublicOfficialSelectionService.CancelCheckPublicOfficialRecordModelCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("撤销成功!");
            this.DialogResult = true;
        }

        /// <summary>
        /// 审核通过
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (isLock == "1")
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("该职位已锁定!");
                return;
            }
            if (MessageBox.Show("确定审核通过吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                //client.CheckAsync(_idcard, _name, 2,_positionid);
                client.CheckPublicOfficialRecordModelCompleted -=
                    new EventHandler<PublicOfficialSelectionService.CheckPublicOfficialRecordModelCompletedEventArgs>(
                        Client1_CheckPublicOfficialRecordModelCompleted);
                client.CheckPublicOfficialRecordModelCompleted +=
                    new EventHandler<PublicOfficialSelectionService.CheckPublicOfficialRecordModelCompletedEventArgs>(
                        Client1_CheckPublicOfficialRecordModelCompleted);
                client.CheckPublicOfficialRecordModelAsync("(" + _PublicOfficialRecordID.ToString() + ")",_type);
            }
        }

        void Client1_CheckPublicOfficialRecordModelCompleted(object sender, PublicOfficialSelectionService.CheckPublicOfficialRecordModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("审核通过!");
                this.DialogResult = true;
            }
            else
                SilverlightMessageBoxLibrary.Message.ErrorMessage("失败:" + _name + "已有岗位通过审核!");
        }

        /// <summary>
        /// 退回
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (isLock == "1")
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("该职位已锁定!");
                return;
            }
            //client.CheckAsync(_idcard, _name, 3, _positionid);
            TuiHuiMemoWindow tuihui = new TuiHuiMemoWindow();
            tuihui.Closed += new EventHandler(tuihui_Closed);
            tuihui.Show();
        }

        void tuihui_Closed(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定退回吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                client.TuiPublicOfficialRecordModelCompleted -=
                   new EventHandler<PublicOfficialSelectionService.TuiPublicOfficialRecordModelCompletedEventArgs>(Client1_TuiPublicOfficialRecordModelCompleted);
                client.TuiPublicOfficialRecordModelCompleted +=
                    new EventHandler<PublicOfficialSelectionService.TuiPublicOfficialRecordModelCompletedEventArgs>(Client1_TuiPublicOfficialRecordModelCompleted);
                client.TuiPublicOfficialRecordModelAsync("(" + _PublicOfficialRecordID.ToString() + ")",
                                                          ((TuiHuiMemoWindow)sender).checkmemo);
            }
        }

        void Client1_TuiPublicOfficialRecordModelCompleted(object sender, PublicOfficialSelectionService.TuiPublicOfficialRecordModelCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("退回成功!");
            this.DialogResult = true;
        }

        private EmpbasicService.EmpBasicServiceClient _empClient = null;
        /// <summary>
        /// 导出员工简历
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExport_Click(object sender, RoutedEventArgs e)
        {
            _empClient.ExportPersonnelInfoPositionAsync(_empid, _positionid);


        }
	}
}